/*
 * uart_debug.c
 *
 *  Created on: 2021年8月27日
 *      Author: Administrator
 */

#include "board.h"
#include "uart_rs485.h"
#include "mlog.h"
#include "settings.h"
#include <string.h>
#include "string_tools.h"
#include "soft_timer.h"
#include <meter.h>
#include "main.h"

SerialBuf rs485_uart_buf = {{0}, 0, 0, 0};

void rs485_uart_buf_init(void)
{
	memset(rs485_uart_buf.DataBuf, 0, SERIAL_MAX_BUF_SIZE);
	rs485_uart_buf.DataLen = 0;
	rs485_uart_buf.recFlag = 0;
	rs485_uart_buf.RecTime = 0;
}

void rs485_uart_receive(uint8_t data)
{
	rs485_uart_buf.DataBuf[rs485_uart_buf.DataLen++] = data;
	rs485_uart_buf.RecTime = getSystemTime();
}

void rs485_uart_receive_check(void)
{
	if(rs485_uart_buf.recFlag==1)
    return;
  if(rs485_uart_buf.DataLen==0)
    return;
  if((getSystemTime()-rs485_uart_buf.RecTime) > LOG_UART_REC_TIMEOUT)
    rs485_uart_buf.recFlag=1;
}

void rs485_uart_handle(void)
{
	uint8_t id[4] = {0};
	uint8_t device[20] = {0}; 
	uint8_t re_data[22] = {0};
	uint8_t biao[12] = {0};
	uint8_t meterCnt[10]={0};
	uint32_t temp = 0;
	uint32_t temp2 = 0;
	uint8_t mm = 0;
	uint32_t bat=0;
	uint64_t ddz=0;
	
	if(rs485_uart_buf.recFlag)
	{
		if(rs485_uart_buf.DataLen > 0)
		{
			
			mprintfhex("接收数据:", rs485_uart_buf.DataBuf, rs485_uart_buf.DataLen);
			#if 1
			if((rs485_uart_buf.DataBuf[0] == 0x5a) && (rs485_uart_buf.DataBuf[1] == 0x80) && (rs485_uart_buf.DataBuf[rs485_uart_buf.DataLen - 1] == 0x0d))//////服务器操作水表标志
			{
				lora_auto_reboot_time_reset();
				memcpy(device,SystemSetting[2].nodePtr,12);
				id[0]=(device[4]-0x30)*10+(device[5]-0x30);
				id[1]=(device[6]-0x30)*10+(device[7]-0x30);
				id[2]=(device[8]-0x30)*10+(device[9]-0x30);
				id[3]=(device[10]-0x30)*10+(device[11]-0x30); 
				if(((rs485_uart_buf.DataBuf[2] == 0xaa) && 
						(rs485_uart_buf.DataBuf[3] == 0xaa) && 
						(rs485_uart_buf.DataBuf[4] == 0xaa) && 
						(rs485_uart_buf.DataBuf[5] == 0xaa)) || 
						((rs485_uart_buf.DataBuf[2] == id[0]) && 
						(rs485_uart_buf.DataBuf[3] == id[1]) && 
						(rs485_uart_buf.DataBuf[4] == id[2]) && 
						(rs485_uart_buf.DataBuf[5] == id[3])))   /////万表表号操作
				{
					 if(rs485_uart_buf.DataBuf[6]==0xe1)  /////数据采集
					 {
						 	//memcpy(device,SystemSetting[2].nodePtr,12);
						  //EEPROM_ReadBytes(EEPROM_WATTER1_ADDR,meterCnt,5);
									  ////取两个数中比较大的数
						  temp = meter_get_count1();
						  if(meter_get_count2()>temp)
							{
								temp=meter_get_count2();
								temp2=meter_get_count1();
							}
							else 
							{
								temp2=meter_get_count2();
							}
							
							///////////////////如果出现有一路数据很异常，存储少的数据
							if(temp-temp2>5000) 
							{
							   sprintf(meterCnt, "%d", temp2);
							   meter_cnt1_clear();
							   meter_cnt2_clear();						 
							   System_SettingsSetSingle(SETTINGS_METER_CNT1 , 4, meterCnt); 
							   System_SettingsSetSingle(SETTINGS_METER_CNT2 , 4, meterCnt); 
							   return;
							}
							
						  //temp = meterCnt[0]+meterCnt[1]+meterCnt[2]+meterCnt[3];
						  /*取模*/
								bat=bat_get_votage();
								re_data[0] = 0x5a;
								re_data[1] = 0x00;
								re_data[2] = id[0];
								re_data[3] = id[1];
								re_data[4] = id[2];
								re_data[5] = id[3]; 
								re_data[6] = 0xe1;
								re_data[8] = (temp>>24);
								re_data[9] = (temp>>16); 
								re_data[10] = (temp>>8);   
								re_data[11] = temp; 
								re_data[12] = bat>>24;
								re_data[13] = bat>>16; 
								re_data[14] = bat>>8;    
								re_data[15] = bat;  
								re_data[16] = (temp2>>24);
								re_data[17] = (temp2>>16); 
								re_data[18] = (temp2>>8);   
								re_data[19] = temp2; 
								re_data[20] = 0xff;                  
								re_data[21] = 0x0d;      
								rs485_uart_send(re_data,22); 							
					 }
					 else if((rs485_uart_buf.DataBuf[6] == 0xe4) && (rs485_uart_buf.DataBuf[8] == 0xaa))   //////开阀命令
					 {
		
						valve_open(); 				
						re_data[0] = 0x5a;
						re_data[1] = 0x00;
						re_data[2] = id[0];
						re_data[3] = id[1];
						re_data[4] = id[2];
						re_data[5] = id[3]; 
						re_data[6] = 0xe4;
						re_data[7] = 0;
						re_data[8] = 0x00; 
						re_data[9] = 0xff;                
						re_data[10] = 0x0d;              
						rs485_uart_send(re_data,11);  
					 }
					 else if((rs485_uart_buf.DataBuf[6]==0xe4) && (rs485_uart_buf.DataBuf[8]==0x55))   //////闭阀命令
					 {
					
						valve_close();
						re_data[0] = 0x5a;
						re_data[1] = 0x00;
						re_data[2] = id[0];
						re_data[3] = id[1];
						re_data[4] = id[2];
						re_data[5] = id[3]; 
						re_data[6] = 0xe4;
						re_data[7] = 0;
						re_data[8] = 0xff; 
						re_data[9] = 0xff;                
						re_data[10] = 0x0d;              
						rs485_uart_send(re_data,11);   						 
					 }	
					 else if(rs485_uart_buf.DataBuf[6] == 0xe6)   /////获得电压
					 {
						bat=bat_get_votage();
						re_data[0]=0x5a;
						re_data[1]=0x00;
						re_data[2]=id[0];
						re_data[3]=id[1];
						re_data[4]=id[2];
						re_data[5]=id[3]; 
						re_data[6]=0xe6;
						re_data[7]=bat>>8;
						re_data[8]=bat; 
						re_data[9]=0xff;                
						re_data[10]=0x0d;              
						rs485_uart_send(re_data,11);      
					 }		
					 else if(rs485_uart_buf.DataBuf[6] == 0xe2)   /////修改底度
					 {
						ddz=((uint64_t)rs485_uart_buf.DataBuf[7]<<24)+((uint64_t)rs485_uart_buf.DataBuf[8]<<16)+((uint64_t)rs485_uart_buf.DataBuf[9]<<8)+rs485_uart_buf.DataBuf[10];

						sprintf(meterCnt, "%ld", ddz);	
											 
						meter_cnt1_clear();
						meter_cnt2_clear();						 
						System_SettingsSetSingle(SETTINGS_METER_CNT1 , 10, meterCnt); 
						System_SettingsSetSingle(SETTINGS_METER_CNT2 ,10, meterCnt); 						 
								 
						re_data[0] = 0x5a;
						re_data[1] = 0x00;
						re_data[2] = id[0];
						re_data[3] = id[1];
						re_data[4] = id[2];
						re_data[5] = id[3]; 
						re_data[6] = 0xe2;
						re_data[7] = 0xff;                
						re_data[8] = 0x0d;              
						rs485_uart_send(re_data,9);   					 
					 }	
					 else if(rs485_uart_buf.DataBuf[6] == 0xe3)   /////修改表号
					 {						  						 
						   biao[0] = 0x35;
						   biao[1] = 0x31;
						   biao[2] = 0x30;
						   biao[3] = 0x30;              
						   biao[4] = rs485_uart_buf.DataBuf[7] / 10 + 0x30;
						   biao[5] = rs485_uart_buf.DataBuf[7] % 10 + 0x30;
						   biao[6] = rs485_uart_buf.DataBuf[8] / 10 + 0x30;
						   biao[7] = rs485_uart_buf.DataBuf[8] % 10 + 0x30;
						   biao[8] = rs485_uart_buf.DataBuf[9] / 10 + 0x30;
						   biao[9] = rs485_uart_buf.DataBuf[9] % 10 + 0x30;
						   biao[10] = rs485_uart_buf.DataBuf[10] / 10 + 0x30;
						   biao[11] = rs485_uart_buf.DataBuf[10] % 10 + 0x30;              
						   System_SettingsSetSingle(SETTINGS_DEVICE_SN, 12, biao); 
							re_data[0] = 0x5a;
							re_data[1] = 0x00;
							re_data[2] = id[0];
							re_data[3] = id[1];
							re_data[4] = id[2];
							re_data[5] = id[3]; 
							re_data[6] = 0xe3;
							re_data[7] = 0xff;                
							re_data[8] = 0x0d;              
							rs485_uart_send(re_data,9); 
							soft_reboot(); 								
					 }		
				}
			}
			#endif
		}
		
		rs485_uart_buf_init();
	}
}




